package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.AttendanceRecord;
import com.ruoyi.system.service.IAttendanceRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 出勤记录Controller
 * 
 * @author ruoyi
 * @date 2022-06-07
 */
@RestController
@RequestMapping("/system/record")
public class AttendanceRecordController extends BaseController
{
    @Autowired
    private IAttendanceRecordService attendanceRecordService;

    /**
     * 查询出勤记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(AttendanceRecord attendanceRecord)
    {
        Long roleId = getLoginUser().getUser().getRoleId();
        if (roleId != 1) {
            attendanceRecord.setUserId(getUserId());
        }
        startPage();
        List<AttendanceRecord> list = attendanceRecordService.selectAttendanceRecordList(attendanceRecord);
        return getDataTable(list);
    }

    /**
     * 导出出勤记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:record:export')")
    @Log(title = "出勤记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AttendanceRecord attendanceRecord)
    {
        List<AttendanceRecord> list = attendanceRecordService.selectAttendanceRecordList(attendanceRecord);
        ExcelUtil<AttendanceRecord> util = new ExcelUtil<AttendanceRecord>(AttendanceRecord.class);
        util.exportExcel(response, list, "出勤记录数据");
    }

    /**
     * 获取出勤记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(attendanceRecordService.selectAttendanceRecordById(id));
    }

    /**
     * 新增出勤记录
     */
    @PreAuthorize("@ss.hasPermi('system:record:add')")
    @Log(title = "出勤记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AttendanceRecord attendanceRecord)
    {
        return toAjax(attendanceRecordService.insertAttendanceRecord(attendanceRecord));
    }

    /**
     * 修改出勤记录
     */
    @PreAuthorize("@ss.hasPermi('system:record:edit')")
    @Log(title = "出勤记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AttendanceRecord attendanceRecord)
    {
        return toAjax(attendanceRecordService.updateAttendanceRecord(attendanceRecord));
    }

    /**
     * 删除出勤记录
     */
    @PreAuthorize("@ss.hasPermi('system:record:remove')")
    @Log(title = "出勤记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(attendanceRecordService.deleteAttendanceRecordByIds(ids));
    }
}
